package jianzhioffer;

import java.util.*;

// 剑指 Offer 32 - I. 从上到下打印二叉树
public class LevelOrder {

    // 就是层次遍历，用BFS即可
    public int[] levelOrder(TreeNode root) {
        // 特判
        if (root == null) {
            return new int[0];
        }
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        List<Integer> list = new ArrayList<>();
        while (!deque.isEmpty()) {
            TreeNode res = deque.poll();
            list.add(res.val);
            if (res.left != null) {
                deque.offer(res.left);
            }
            if (res.right != null) {
                deque.offer(res.right);
            }

        }
//        int[] ans = new int[list.size()];
//        for (int i = 0; i < list.size(); i++) {
//            ans[i] = list.get(i);
//        }
//        return ans;
        // 这是集合转为基本数据类型数组的方法
        return list.stream().mapToInt(Integer::intValue).toArray();

    }


    // 剑指 Offer 32 - II. 从上到下打印二叉树 II
    public List<List<Integer>> levelOrder2(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                list.add(node.val);
                if (node.left != null) {
                    deque.offer(node.left);
                }
                if (node.right != null) {
                    deque.offer(node.right);
                }
            }
            res.add(list);
        }
        return res;
    }

    // 剑指 Offer 32 - II. 从上到下打印二叉树 II   ----- 递归版
    public List<List<Integer>> levelOrder21(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();

        dfs(root, res, 0);
        return res;
    }

    private void dfs(TreeNode root, List<List<Integer>> res, int depth) {
        if (root == null) {
            return;
        }
        if (res.size() == depth) {
            res.add(new ArrayList<>());
        }
        res.get(depth).add(root.val);
        dfs(root.left, res, depth + 1);
        dfs(root.right, res, depth + 1);

    }


    // 剑指 Offer 32 - III. 从上到下打印二叉树 III
    public List<List<Integer>> levelOrder3(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                list.add(node.val);
                if (node.left != null) {
                    deque.add(node.left);
                }
                if (node.right != null) {
                    deque.add(node.right);
                }
            }
            res.add(list);
        }
        for (int i = 1; i < res.size(); i += 2) {
            Collections.reverse(res.get(i));
        }
        return res;
    }


    // 剑指 Offer 32 - III. 从上到下打印二叉树 III
    public List<List<Integer>> levelOrder31(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                // 因为res的size从0起步的，所以和你想的是相反的
                if (res.size() % 2 == 0) {
                    list.add(node.val);
                } else {
                    list.add(0, node.val);
                }

                if (node.left != null) {
                    deque.add(node.left);
                }
                if (node.right != null) {
                    deque.add(node.right);
                }
            }
            res.add(list);
        }

        return res;
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
